<template>
	<view class="m-input">
<!--		<view class="m-input-title" :style="titleStyle">{{ title }}</view>-->
		<view class="flex_r fa_c m-input-box">
			<view class="flex1 input">
				<input v-model="inputValue" type="text" :focus="focus" :placeholder="placeholder" :password="password"
					@input="input" @confirm="confirm" v-if="type == 'text'" />
				<input v-model="inputValue" type="number" :focus="focus" :placeholder="placeholder" @input="input"
					@confirm="confirm" v-else />
			</view>
<!--			<view class="m-input-box-icon" @click="empty" v-if="inputValue">-->
<!--				<image class="img"-->
<!--					src=""-->
<!--					mode="aspectFill"></image>-->
<!--			</view>-->
			<view class="m-input-box-r">
				<slot />
			</view>
		</view>
		<view class="m-line"></view>
		<view class="text_colour_red m-input-text" v-if="error_msg">{{ error_msg }}</view>
	</view>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue'

const props = defineProps({
  modelValue: {
    type: [String, Number],
    default: ''
  },
  type: {
    type: String,
    default: 'text'
  },
  focus: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '标题'
  },
  titleStyle: {
    type: String,
    default: ''
  },
  placeholder: {
    type: String,
    default: '请输入ID/手机号'
  },
  password: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'confirm'])

const error_msg = ref('')
const inputValue = ref('')

watch(() => props.modelValue, (val) => {
  inputValue.value = val
}, { immediate: true })

const errorText = (msg) => {
  error_msg.value = msg
}

const empty = () => {
  inputValue.value = ''
  emit('update:modelValue', '')
}

const confirm = () => {
  emit('confirm')
}

const input = (e) => {
  if (error_msg.value) {
    error_msg.value = ''
  }
  emit('update:modelValue', e.detail.value)
}

defineExpose({ errorText })
</script>

<style scoped lang="scss">
	.m-line {
		width: 100%;
		transform: scaleY(0.5);
	}

	.m-input {
		box-sizing: border-box;
		position: relative;
		width: 100%;
		padding-bottom: 10rpx;

		.m-input-title {
			width: 100%;
			height: 54rpx;
			font-size: 34rpx;
			font-weight: bold;
		}

		.m-input-box {
			width: 100%;
			height: 60rpx;
			margin-bottom: 10rpx;

			.input {
				box-sizing: border-box;
				padding-left: 4rpx;
				height: 100%;
        text-align: right;

				input {
					width: 100%;
					height: 100%;
				}
			}

			.m-input-box-icon {
				width: 40rpx;
				height: 40rpx;
				margin-right: 15rpx;
				.img{
					width: 100%;
					height: 100%;
				}
			}

			.m-input-box-r {}
		}

		.m-input-text {
			position: absolute;
			bottom: -8rpx;
			right: 0;
			font-size: 24rpx;
			margin-top: 6rpx;
			animation: shake 0.8s ease-in-out;
		}
	}

	@keyframes shake {

		/* 水平抖动，核心代码 */
		10%,
		90% {
			transform: translate3d(-1px, 0, 0);
		}

		20%,
		80% {
			transform: translate3d(+2px, 0, 0);
		}

		30%,
		70% {
			transform: translate3d(-4px, 0, 0);
		}

		40%,
		60% {
			transform: translate3d(+4px, 0, 0);
		}

		50% {
			transform: translate3d(-4px, 0, 0);
		}
	}
	.text_colour_red {
		color: #ff0000;
	}
	.flex_r {
		display: flex;
		flex-direction: row;
	}
	
	.fa_c {
		align-items: center;
	}
	.flex1 {
		flex: 1;
	}
</style>